﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc.Html.jTable;

namespace System.Web.Mvc.Html
{
    public static class jTableExtention
    {
        public static MvcHtmlString jTable(this HtmlHelper htmlHelper, jTableOptions options)
        {
            var scope = "$('#" + options.General.ContainerHtmlElementID + "'";
            var txt = new StringBuilder();
            txt.AppendLine("<script type='text/javascript'>");
            txt.AppendLine("$(document).ready(function () {");
            txt.AppendLine(jTableDefinition(options));
            txt.AppendFormat("$('#{0}').jtable('load');", options.General.ContainerHtmlElementID);
            txt.AppendLine();
            txt.AppendLine("});");
            txt.AppendLine("</script>");
            return new MvcHtmlString(txt.ToString());
        }

        private static String jTableDefinition(jTableOptions options)
        {
            var txt = new StringBuilder();
            txt.AppendFormat(" $('#{0}').jtable(", options.General.ContainerHtmlElementID);
            txt.AppendLine("{");
            txt.AppendLine(jTableDefGeneralOption(options.General));
            txt.AppendLine(",actions:{listAction: '" + options.Action.ListUrl + "'},");
            txt.AppendLine(jTableDefFieldsOption(options.Fields));
            txt.AppendLine(" });");
            return txt.ToString();
        }

        private static String jTableDefGeneralOption(OptionGeneral general)
        {
            var txt = new StringBuilder();
            txt.AppendFormat("paging:{0},", general.PagingEnabled.ToString().ToLower());
            txt.AppendFormat("pageSize:{0},", general.PageSize);            
            txt.AppendFormat("selecting:true,");
            txt.AppendFormat("multiselect:{0},", general.MultiSelect.ToString().ToLower());
            txt.AppendFormat("selectingCheckboxes:{0},", general.CheckBoxColumn.ToString().ToLower());
            txt.AppendFormat("defaultSorting:\"{0}\",", general.DefaultSortingColumnName + " " + general.DefaultSortingOrder.ToString());
            txt.AppendFormat("sorting:true");            
            return txt.ToString();
        }

        private static String jTableDefFieldsOption(List<OptionField> fields)
        {
            var temp = new OptionField();
            var txt = new StringBuilder();
            txt.AppendLine("fields: {");
            for (int i = 0; i < fields.Count; i++)
            {
                temp = fields[i];
                txt.AppendLine(temp.Name + ": {");
                if (temp.Key)
                {
                    txt.AppendLine("key: true,");
                }
                if (!String.IsNullOrWhiteSpace(temp.Caption))
                {
                    txt.AppendFormat("title: '{0}',", temp.Caption);
                    txt.AppendLine();
                }
                if (!String.IsNullOrWhiteSpace(temp.Width))
                {
                    txt.AppendFormat("width: '{0}',", temp.Width);
                    txt.AppendLine();
                }
                if (temp.DataType != FieldDataType.None)
                {
                    txt.AppendFormat("type: '{0}',", temp.DataType.ToString().ToLower());
                    txt.AppendLine();
                }
                if (!String.IsNullOrWhiteSpace(temp.Format))
                {
                    txt.AppendFormat("displayFormat: '{0}',", temp.Format);
                    txt.AppendLine();
                }
                if (!String.IsNullOrWhiteSpace(temp.DisplayFunction))
                {
                    txt.AppendFormat("display: {0},", temp.DisplayFunction);
                    txt.AppendLine();
                }
                txt.AppendLine("sorting: " + temp.SortingEnabled.ToString().ToLower());
                txt.Append("}");
                if (i != (fields.Count - 1))
                {
                    txt.AppendLine(",");
                }
            }
            txt.AppendLine("},selectionChanged: function () {}");
            return txt.ToString();
        }

    }
}
